package com.swing.CardLauoyt;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


public class MyFrame extends JFrame
{
	JPanel card = new JPanel();
	JComboBox<String> options = new JComboBox<>();

	public MyFrame(String title)
	{
		super(title);
		
		Container contentPane = getContentPane();
		
		// 设置为边界布局 BorderLayout
		contentPane.setLayout(new BorderLayout());

		//创建一个下拉列表，供选择
		options.addItem("第一个面板");
		options.addItem("第二个面板");

		contentPane.add(options, BorderLayout.PAGE_START);

		//创建第一个面板
		JPanel panel = new JPanel();
		panel.add(new JButton("红"));
		panel.add(new JButton("绿"));
		panel.add(new JButton("蓝"));

		//创建第二个面板
		JPanel p2 = new JPanel();
		p2.add(new JLabel("输入"));
		p2.add(new JTextField(20));

		contentPane.add(card, BorderLayout.CENTER);
		card.setLayout(new CardLayout());
		card.add(panel, "button");
		card.add(p2, "text");

		//添加事件响应
		options.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {

			}
		});
	}

	private void itemchanged(){
		CardLayout cardLayout = (CardLayout)card.getLayout();

		int index = options.getSelectedIndex();
		if(index == 0) {
			cardLayout.show(card, "button");
		} else if(index == 1){
			cardLayout.show(card, "text");
		}
	}
	

	
	
}
